package com.zzj.utils;

import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;


import java.util.HashMap;
import java.util.Map;

@Component
public class WechatUtil {

    @Resource
    private HttpUtil httpUtil;


    public JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";

        MultiValueMap<String, String> requestUrlParams = new LinkedMultiValueMap<>();
//        JSONObject jsonObject =new JSONObject();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
        //小程序appId
        requestUrlParams.add("appid", "wxbb29ee204e0bb294");
        //小程序secret
        requestUrlParams.add("secret", "77795b109c0ab4c1b5f4bf823ed32ba0");
        //小程序端返回的code
        requestUrlParams.add("js_code", code);
        //默认参数
        requestUrlParams.add("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        Map<String, String> headerMap = new HashMap<>();
        String s = httpUtil.postFormData(requestUrl, requestUrlParams);
        JSONObject post = JSONObject.parseObject(s);
        return post;
    }
}
